package com.forum.controller.user;

import com.forum.constant.JwtClaimsConstant;
import com.forum.constant.MessageConstant;
import com.forum.dto.ThreadsDTO;
import com.forum.properties.JwtProperties;
import com.forum.result.Result;
import com.forum.service.ThreadsService;
import com.forum.utils.JwtUtil;
import com.forum.vo.*;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("UserThreadsController")
@Api(tags = "帖子接口")
@RequestMapping("/user/threads")
@Slf4j
public class ThreadsController {
    @Autowired
    private ThreadsService threadsService;
    @Autowired
    private JwtProperties jwtProperties;
    @ApiOperation("发布帖子")
    @PostMapping("/publish")
    public Result publish(@RequestBody ThreadsDTO threadsDTO){
        log.info("用户发布帖子{}",threadsDTO);
        threadsService.publish(threadsDTO);
        return Result.success(MessageConstant.INSERT_SUCCESS);
    }

    @ApiOperation("最新文章")
    @GetMapping("/newArticles")
    public Result<List<ThreadsVO>> ArticleList(){
        List<ThreadsVO> result = threadsService.ArticleList();
        return Result.success(result);
    }

    @ApiOperation("热门话题")
    @GetMapping("/hotTopics")
    public Result<List<TopicsVO>> hotTopics(){
        List<TopicsVO> result = threadsService.TopicList();
        return Result.success(result);
    }

    @ApiOperation("精选文章")
    @GetMapping("/excellentArticles")
    public Result<List<ClassicArticlesTop3VO>> excellentArticles(){
        List<ClassicArticlesTop3VO> result = threadsService.articleTop3();
        return Result.success(result);
    }

    @ApiOperation("热门文章")
    @GetMapping("/hotArticles")
    public Result<List<ArticlesAndTopicsVO>> ArticlesAndTopics(Long categoryId, Integer type){
        log.info("传过来的分类id {}",categoryId);
        List<ArticlesAndTopicsVO> result = threadsService.ArticlesAndTopics(categoryId, type);
        return Result.success(result);
    }

    @ApiOperation("最新文章")
    @GetMapping("/latestArticle")
    public Result<List<ArticlesAndTopicsVO>> latestArticlesAndTopics(Long categoryId, Integer type){
        log.info("传过来的分类id {}",categoryId);

        List<ArticlesAndTopicsVO> result = threadsService.latestArticle(categoryId,type);
        return Result.success(result);
    }

    @ApiOperation("我的帖子")
    @GetMapping("/myThreads")
    public Result<List<MyThreadsVO>> myThreads(String token){
        log.info("传过来的token,{}",token);
        Claims claims = JwtUtil.parseJWT(jwtProperties.getAdminSecretKey(), token);
        Long userId = Long.valueOf(claims.get(JwtClaimsConstant.USER_ID).toString());
        log.info("解析过,{}",userId);
        List<MyThreadsVO> result = threadsService.myThreads(userId);
        return Result.success(result);
    }

}
